const echarts = require('echarts');

function eChart() {
    return {
        restrict: 'EA',
        link: link
    }

    function link(scope, element, attrs) {
        var chart = echarts.init(element[0]);

        scope.$watch(attrs['eOption'], function() {
            var option = scope.$eval(attrs.eOption);
            if (angular.isObject(option))
                chart.setOption(option);
        }, true);

        scope.getDom = function() {
            return {
                'height': element[0].offsetHeight,
                'width': element[0].offsetWidth
            };
        }

        scope.$watch(scope.getDom, function(n) {
            chart.resize();
        }, true);

        //事件绑定
        function bindevent() {
            if (angular.isArray(scope.config.event)) {
                angular.forEach(scope.config.event, function(value, key) {
                    for (var e in value) {
                        chart.on(e, value[e]);
                    }
                });
            }
        }
    }
}

module.exports = eChart;
